package com.pvdnc.nsocket.client;

import com.pvdnc.nsocket.log.SLog;
import com.pvdnc.nsocket.protocol.Interpreter;
import com.pvdnc.nsocket.protocol.PacketCodec;
import com.pvdnc.nsocket.protocol.StreamInterpreter;
import com.pvdnc.nsocket.protocol.base.Packet;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public abstract class SimpleClientHandler<C extends BaseClient,I extends Packet> extends SimpleChannelInboundHandler<I> {
    private static final SLog LOG=new SLog(SimpleClientHandler.class);

    private final C mClient;

    private String mCommand;

    public C getServer() {
        return mClient;
    }

    public SimpleClientHandler(C client, String commmand){
        this(client,new StreamInterpreter(commmand));
    }

    public <T extends Interpreter> SimpleClientHandler(C client,T interpreter){
        mClient =client;
        PacketCodec.getInstance().addInterpreter(interpreter);
        mCommand=interpreter.getCommand();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, I msg) throws Exception {
        LOG.d("received:"+mCommand+"\n\t"+msg);
        onReceived(ctx, msg);
    }

    public abstract void onReceived(ChannelHandlerContext ctx,I msg);


}
